//
//  InVehicleDetailViewController.m
//  Runone
//
//  Created by runone02 on 16/10/25.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "InVehicleDetailViewController.h"
#import "VehicleListViewController.h"
#import "RequestGinsengModel.h"
#import "RequestOutginseng.h"
#import "RequestHelper.h"
#import "AppDelegate.h"
#import "MJExtension.h"
#import "IntVclDetailInfoModel.h"
#import "OneInVcDetailHeaderView.h"
#import "IntVclDrivedRecordInfo.h"
#import "SVProgressHUD.h"
#import "VehiclePlanDetailViewController.h"
#import "UIView+Addition.h"
#import "EnlargeMapViewController.h"
#import "OneConst.h"
#import "InquiryRecordViewController.h"
#import "IntVclDynamicHistoryModel.h"
#import "CarManagerHistoryViewController.h"
#import "RightCodeTool.h"
#import "InternalVehicleTrajectoryListViewController.h"
#import "RODateHelper.h"
#import "NSDate+ROCaculateTime.h"
#import "EKDateTimePickerView.h"
#import "UIColor+Addition.h"
#import "NSDate+Extension.h"
#import "EKDateTimePickerView.h"

@interface InVehicleDetailViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
//@property (nonatomic, strong) NSArray *inVcDetailArray;
@property (nonatomic, strong) IntVclDetailInfoModel *intVCDetailModel;
@property (nonatomic, strong) OneInVcDetailHeaderView *inVcDetailV;

@property (nonatomic, assign) NSInteger carType;

@property (nonatomic, assign) BOOL inquiryBntIsClick; //按钮已经被点击

// ******************************* - 查询的数组 - *******************************
@property (nonatomic, strong) NSMutableArray *searchDataArr; //查询的数组

@property (nonatomic, assign) AllIconType type;
//@property (nonatomic, strong) NSDate *beginD;
//@property (nonatomic, strong) NSDate *endD;
@property (nonatomic, strong) NSDate *lastScrollDate;// 上一次滚动的时间
@end

static NSString *cellID = @"OneInVehicleCell";
@implementation InVehicleDetailViewController

#pragma     - ----------车辆信息

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    _searchDataArr=[[NSMutableArray alloc] init];
//    _carType = _carModel.VehicleType;
    if (_carModel) {
        _carType = _carModel.VehicleType;
    }
    if (_listModel) {
        _carType = _listModel.VehicleType;
    }
    
    _intVCDetailModel = [[IntVclDetailInfoModel alloc] init];
    [self confiNavigationBar];
    [self requestData];
    [self configTableView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden            = YES;
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark - 导航栏
- (void) confiNavigationBar {
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:(68.0 / 255) green:(169.0 / 255) blue:(213.0 / 255) alpha:1.000]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.size     = CGSizeMake(100, 30);
    
    if (_carModel.VehicleNo) {
         titleLabel.text          = _carModel.VehicleNo;
    }
    
    if (_listModel.VehicleNo) {
        titleLabel.text     = _listModel.VehicleNo;
    }
    
    titleLabel.textColor          = [UIColor whiteColor];
    self.navigationItem.titleView = titleLabel;
}
#pragma mark - 请求数据
- (void) requestData {
    RequestHelper *requestHelper = [[RequestHelper alloc] init];
    RequestGinsengModel *getInVcDetailModel = [[RequestGinsengModel alloc] init];
    getInVcDetailModel.URL                  = GetIntVclBaseInfo;
    if (_carModel) {
        getInVcDetailModel.DataValue            = [self DataValueVehicleNO:_carModel.VehicleNo];
    }
    if (_listModel) {
        getInVcDetailModel.DataValue            = [self DataValueVehicleNO:_listModel.VehicleNo];
    }
    getInVcDetailModel.UpdatedTime          = @"";
    getInVcDetailModel.SystemCode           = @"GuangleHighway";
    getInVcDetailModel.MethodName           = @"GetIntVclDetailInfo";
    requestHelper.block = ^(RequestOutginseng *requestOutginseng,id dataValueModel) {

        _intVCDetailModel = [IntVclDetailInfoModel mj_objectWithKeyValues:requestOutginseng.DataValue];
        if (_carModel) {
            [_inVcDetailV setIntData:_intVCDetailModel carData:_carModel andListData:nil];
        }else if (_listModel) {
         [_inVcDetailV setIntData:_intVCDetailModel carData:nil andListData:_listModel];
        }
    };
    [requestHelper RequestGinsengModel:getInVcDetailModel andPushController:self];
}
#pragma mark - 配置tableview
- (void) configTableView {
    _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, CGRectGetHeight(self.view.frame) - 64) style:UITableViewStylePlain];
    _tableview.bounces   = NO;
    _inVcDetailV = [[OneInVcDetailHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width,630)];
    
    [_inVcDetailV.beginTimeBtn addTarget:self action:@selector(beginTimeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_inVcDetailV.endTimeBtn   addTarget:self action:@selector(endTimeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_inVcDetailV.inquiryBtn   addTarget:self action:@selector(inquiryBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_inVcDetailV.enlargeBtn   addTarget:self action:@selector(enlargeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_inVcDetailV.historyBtn   addTarget:self action:@selector(historyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _tableview.tableHeaderView = _inVcDetailV;
    [self.view addSubview:_tableview];
}
#pragma  mark - 监听按钮点击
- (void) historyBtnClick {
    if (![[RightCodeTool sharedTool] rightIsEqualTO:@"P030102"]) {
        [SVProgressHUD showErrorWithStatus:@"没有该权限"];
        return;
    }
    InquiryRecordViewController *recordVC = [[InquiryRecordViewController alloc] init];
    if (_carModel.VehicleNo) {
         recordVC.vehicleNO         = _carModel.VehicleNo;
    }
    
    if (_listModel.VehicleNo) {
         recordVC.vehicleNO     = _listModel.VehicleNo;
    }

    [self.navigationController pushViewController:recordVC animated:YES];
}
//弹出时间选择器
- (void) setUpDatePicker:(UILabel *)label {
    EKDateTimePickerView *datepicker = [[EKDateTimePickerView alloc]initWithDatePosition:EKDatePositionBottom CompleteBlock:^(NSDate *startDate) {
        _lastScrollDate = startDate;
        label.text = [startDate stringWithOutTimeWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    }];
    datepicker.datePickerStyle = EKDateStyleShowYearMonthDayHourMinute;
    datepicker.themeColor      = [UIColor colorWithHexString:@"38b1dc"];
    if (label.text != nil) {
        _lastScrollDate = nil;
        [datepicker scrollToSepicefyDate:[RODateHelper dateWithOutTimeZoneFromString:label.text withFormat:@"yyyy-MM-dd HH:mm:ss"] animated:NO];
    }
    if (_lastScrollDate != nil) {
        [datepicker scrollToSepicefyDate:_lastScrollDate animated:NO];
    }
    [datepicker show];
}

//开始时间
- (void) beginTimeBtnClick {
    [self setUpDatePicker:_inVcDetailV.beginTimeL];
}

//结束时间
- (void) endTimeBtnClick {
    [self setUpDatePicker:_inVcDetailV.endTimeL];
}

//放大地图
- (void) enlargeBtnClick {
    EnlargeMapViewController *vc = [[EnlargeMapViewController alloc] init];
    switch (_carType) {
        case 1: {
            if ([_intVCDetailModel.OD_Rate integerValue] == 0&& [_carModel.OD_Rate integerValue]==0) {
                _type = AllIconTypeRoadAdminZero;
            }else {
            _type = AllIconTypeRoadAdmin;
            }
        }
            break;
            
        case 2: {
            if ([_intVCDetailModel.OD_Rate integerValue] == 0&& [_carModel.OD_Rate integerValue]==0) {
                _type = AllIconTypeRecusZero;
            }else {
                _type = AllIconTypeRecus;
            }
        }
            break;
            
        case 3:
            if ([_intVCDetailModel.OD_Rate integerValue] == 0 && [_carModel.OD_Rate integerValue] == 0 ) {
//                annotationView.image = [UIImage imageNamed:@"conservation_stop"];//养护停
                _type = AllIconTypeConversationStop;
            }else {
//                annotationView.image = [UIImage imageNamed:@"conservation_move"];//养护
                _type = AllIconTypeConversation;
            }
            break;
            
        case 4:
            break;
        default: {
            if ([_intVCDetailModel.OD_Rate integerValue] == 0 && [_carModel.OD_Rate integerValue]==0) {
                _type = AllIconTypeAdminZero;
            }else {
            _type = AllIconTypeAdmin;
            }
        }
            break;
    }
    vc.type = _type;
    
    if (_carModel.Latitude && _carModel.Longitude) {
        vc.la   = _carModel.Latitude;
        vc.lo   = _carModel.Longitude;
    }
    if ([_intVCDetailModel.Latitude integerValue] && [_intVCDetailModel.Longitude integerValue]) {
        vc.la = [_intVCDetailModel.Latitude floatValue];
        vc.lo = [_intVCDetailModel.Longitude floatValue];
    }
   
    [self presentViewController:vc animated:YES completion:nil];

}
#pragma mark - 查询历史轨迹
- (void) inquiryBtnClick {
    
   if (![[RightCodeTool sharedTool] rightIsEqualTO:@"P030103"]) {
        [SVProgressHUD showErrorWithStatus:@"没有该权限"];
        return;
   }
    

    NSDate *beginDate = [RODateHelper dateWithOutTimeZoneFromString:self.inVcDetailV.beginTimeL.text withFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *endDate = [RODateHelper dateWithOutTimeZoneFromString:self.inVcDetailV.endTimeL.text withFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    NSInteger beginInter = [beginDate compareToCurrentDate];
    if (beginInter == 0) {
        [SVProgressHUD showSuccessWithStatus:@"开始时间不能等于当前时间"];
        return;
    }
    if (beginInter == 1) {
        [SVProgressHUD showSuccessWithStatus:@"开始时间不能大于当前时间"];
        return;
    }
    
    NSInteger endInter = [endDate compareToCurrentDate];
    if (endInter == 1) {
        [SVProgressHUD showSuccessWithStatus:@"结束时间不能大于当前时间"];
        return;
    }
    
    NSInteger beginAndEndCom = [beginDate compareToToDate:endDate];
    if (beginAndEndCom == 0) {
        [SVProgressHUD showSuccessWithStatus:@"开始时间不能等于结束时间"];
        return;
    }
    if (beginAndEndCom == 1) {
        [SVProgressHUD showSuccessWithStatus:@"开始时间不能大于结束时间"];
        return;
    }
       BOOL is24Hour = [beginDate TwoDateIsIn24HourTODate:endDate];
    if (!is24Hour) {
        [SVProgressHUD showSuccessWithStatus:@"结束时间不能超过开始时间24小时"];
        return;
    }
    
    [SVProgressHUD show];
    [self inquiryData];
    
}

- (void) inquiryData {
    InternalVehicleTrajectoryListViewController *vc = [[InternalVehicleTrajectoryListViewController alloc] init];
    if (_carModel) {
        vc.carNum = _carModel.VehicleNo;
    }
    if (_listModel) {
        vc.carNum  = _listModel.VehicleNo;
    }
    vc.beginTime = self.inVcDetailV.beginTimeL.text;
    vc.endTime   = self.inVcDetailV.endTimeL.text;
    [self.navigationController pushViewController:vc animated:YES];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _intVCDetailModel.PicturePath.count;;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
    }
    InVehicleModel *model = _intVCDetailModel.PicturePath[indexPath.row];
    cell.textLabel.text  = model.VehicleNo;
    cell.accessoryType   = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (NSString *)DataValueVehicleNO:(NSString *)VehicleNO  {
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"VehicleNo\",\"value\":\"%@\"}]",VehicleNO];
    return valueStr;
}
//valueStr	__NSCFString *	@"[{\"key\":\"VehicleNo\",\"value\":\"粤A0FU27\"},{\"key\":\"StartDate\",\"value\":\"2017-1-4 20:01:21\"},{\"key\":\"EndDate\",\"value\":\"2017-1-5 19:01:25\"}]"

- (NSString *)dataValueWithVehicleNO:(NSString *)VehicleNO StartDate:(NSString *)begineTime EndDate:(NSString *)endTime {
    NSString *valueStr = [NSString stringWithFormat:@"[{\"key\":\"VehicleNo\",\"value\":\"%@\"},{\"key\":\"StartDate\",\"value\":\"%@\"},{\"key\":\"EndDate\",\"value\":\"%@\"}]", VehicleNO, begineTime, endTime];
    return valueStr;
}


@end
